package cn.lcj.org.controller;

import cn.lcj.basic.annotation.PreAuthorize;
import cn.lcj.basic.utils.AjaxResult;
import cn.lcj.basic.utils.PageList;
import cn.lcj.org.domain.Department;
import cn.lcj.org.domain.Employee;
import cn.lcj.org.query.EmployeeQuery;
import cn.lcj.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * $
 *
 * @return $
 * @date $ $
 * @description $
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    /**
     * 查询单个 用get  url路径传参
     *
     * @param id
     * @return
     * @PathVariable 获取路径参数  不加获取不了数据
     */
    @ApiOperation(value = "接口：查询单个")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个员工", sn = "employee:getOne")
    public Employee findOne(@PathVariable Long id) {
        return employeeService.loadById(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "接口：查询全部")
    @GetMapping
    //http://localhost:8080/employee
    @PreAuthorize(name = "查询全部部门", sn = "employee:getList")
    public List<Employee> findAll() {
        return employeeService.loadAll();
    }

    /**
     * 返回值需要用AjaxResult接收
     * 删除
     *
     * @param id
     * @return
     * @PathVariable url上获取参数
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除单个员工", sn = "empl:del")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 新增与修改
     * 返回值需要用AjaxResult接收 需要返回操作信息
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     *
     * @param employee
     * @return
     */
    @ApiOperation(value = "接口：新增与修改")
    @PutMapping
    @PreAuthorize(name = "保存员工", sn = "employee:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
                return new AjaxResult();
            } else {
                employeeService.update(employee);
                return new AjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }

    }

    /**
     * 分页查询
     * 用封装类的对象做参数来接收
     * 前端对象的接收需要用@RequestBody 从请求体里面获取数据
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "接口：分页查询")
    @PostMapping
    @PreAuthorize(name = "分页", sn = "employee:queryPage")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }

    /**
     * 返回值需要用AjaxResult接收
     * 批量删除
     *
     * @param ids
     * @return
     * @PathVariable url上获取参数
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    @PreAuthorize(name = "批量删除", sn = "employee:patchDelete")
    public AjaxResult patchList(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * @return
     */
    @ApiOperation(value = "接口：无限级数的接口")
    @GetMapping("/deptTree")
    //http://localhost:8080/department
    public List<Department> departmentTree() {
        return employeeService.departmentTree();
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable ("shopId")Long shopId) {
        return employeeService.queryByShopId(shopId);
    }
}
